package com.demo2.todo.service.impl;

import com.demo2.todo.model.entity.SensitiveWord;
import com.demo2.todo.repository.SensitiveWordRepository;
import com.demo2.todo.service.SensitiveWordService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;

/**
 * @author evi
 */
@Service
@RequiredArgsConstructor
public class SensitiveWordServiceImpl implements SensitiveWordService {
    private final SensitiveWordRepository sensitiveWordRepository;

    // 检查内容是否包含敏感词
    @Override
    @Transactional
    public boolean containsSensitiveWords(String content) {
        if (content == null || content.isEmpty()) {
            return false;
        }

        List<String> sensitiveWords = sensitiveWordRepository.findAll().stream()
                .map(SensitiveWord::getWord)
                .toList();

        for (String word : sensitiveWords) {
            if (content.contains(word)) {
                return true;
            }
        }

        return false;
    }
}
